package controller.v2;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import model.AddressVO;
import model.DicVO;
import model.GoodStandardVO;
import model.MemberCartVO;
import model.MemberVO;
import model.OrderVO;
import model.PackageVO;
import model.PromotionItemVO;
import model.PromotionVO;
import service.v2.AddressService;
import service.v2.ConfigService;
import service.v2.DicService;
import service.v2.GoodService;
import service.v2.GoodStandardService;
import service.v2.GoodTempService;
import service.v2.MemberCartServise;
import service.v2.MemberItemService;
import service.v2.MemberService;
import service.v2.MemberTicketService;
import service.v2.OrderService;
import service.v2.PackageService;
import service.v2.PromotionService;
import service.v2.SplitService;
import util.NumberUtil;
import util.PackageUtil;
import util.PropertieUtil;
import util.StringUtil;

@RequestMapping("/v2/membercart")
@Controller
public class MemberCartController {
	
	@Autowired
	private MemberCartServise memberCartServise;
	@Autowired
	private MemberService memberService;
	@Autowired
	private AddressService addressService;
	@Autowired
	private MemberTicketService memberTicketService;
	@Autowired
	private GoodStandardService goodStandardService;
	@Autowired
	private GoodTempService goodTempService;
	@Autowired
	private GoodService goodService;
	@Autowired
	private SplitService splitService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private PromotionService promotionService;
	@Autowired
	private DicService dicService;
	@Autowired
	private MemberItemService memberItemService;
	@Autowired
	private PackageService packageService;
	@Autowired
	private ConfigService configService;
	
	/**
	 * 新购物车List画面
	 * @param token
	 * @param member_id
	 */
	@ResponseBody
	@RequestMapping(value="/newMemberCartList",method=RequestMethod.GET)
	public Object newMemberCartList(@RequestHeader(value = "LANG", required = false) String lang,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response,MemberCartVO memberCartVO, MemberVO memberVO)throws Exception{
		Map<String, Object> map = new HashMap();
		MemberVO cartcity_info = null;
		if(lang==null){
			cartcity_info = memberCartServise.getCartCityInfo(member_id);
		}else{
			cartcity_info = memberCartServise.getCartCityInfoInter(member_id, lang);
		}
		List<MemberCartVO> memberCartList=memberCartServise.selectNewMemberCartList(member_id,cartcity_info.getCity_id3(),lang,cartcity_info.getCity_id0());
		List<PromotionVO> promtion_list = memberCartServise.promtionListById(member_id);
		
		map.put("cart_list", memberCartList);
		map.put("promtionList", promtion_list);
		map.put("cartcity_info", cartcity_info);
		
		return PackageUtil.packageParams(200,"successs",map,""); 
	}
	/**
	 * 购物车CHECK状态更新 
	 * @param cart_id
	 * @param token
	 * @param member_id
	 * @param check
	 */
	@ResponseBody
	@RequestMapping(value="/memberCartCheckUpdate",method=RequestMethod.POST)
	public Object memberCartCheckUpdate(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)String member_id,HttpServletRequest request, HttpServletResponse response, MemberVO memberVO,String check,String cart_id)throws Exception{

		if(cart_id==null || "".equals(cart_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		if(check==null || "".equals(check)){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}else{
			if(!"Y".equals(check)&&!"N".equals(check)){
				return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
			}
		}
		int num = memberCartServise.checkMemberCartId(member_id,cart_id);
		if(num ==0){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		int re = memberCartServise.updateMemberCartCheck(cart_id,check,member_id);
		
		return PackageUtil.packageParams(200,"操作成功","","");
	}
	/**
	 * 购物车删除	 
	 * @param cart_id
	 * @param token
	 * @param member_id
	 */
	@ResponseBody
	@RequestMapping(value="/memberCartDelete",method=RequestMethod.POST)
	public Object memberCartDelete(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,
			HttpServletRequest request, HttpServletResponse response,MemberCartVO memberCartVO, MemberVO memberVO)throws Exception{
		
		if(memberCartVO.getCart_id()==null || "".equals(memberCartVO.getCart_id())){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		int num = memberCartServise.checkMemberCartId(member_id,memberCartVO.getCart_id());
		if(num ==0){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		String[] arrId=memberCartVO.getCart_id().split(",");
		int result = memberCartServise.deleteMemberCarById(arrId,member_id);
		return PackageUtil.packageParams(200,"操作成功","","");
	}
	/**
	 * 购物车修改
	 * @param num
	 * @param cart_id
	 * @param token
	 * @param member_id
	 */
	@ResponseBody
	@RequestMapping(value="/memberCartUpdate",method=RequestMethod.POST)
	public Object memberCartUpdate(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, 
			HttpServletResponse response,MemberCartVO memberCartVO, MemberVO memberVO)throws Exception{
		
		if(memberCartVO.getCart_id()==null || "".equals(memberCartVO.getCart_id())){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		if(memberCartVO.getNum()==null || "".equals(memberCartVO.getNum())){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		int num = memberCartServise.checkMemberCartId(member_id,memberCartVO.getCart_id());
		if(num ==0){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		memberCartVO.setMember_id(member_id);
		int result = memberCartServise.upDateMemberCartVo(memberCartVO);
		
		return PackageUtil.packageParams(200,"操作成功","","");
	}
	
	/**购物车跳转结算页面
	 * @param token
	 * @param member_id
	 * @param cart_ids	 
	 * @param address_id 
	 */
	@ResponseBody
	@RequestMapping(value="/fillOrder",method=RequestMethod.GET)
	public Object fillOrder(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request
			, HttpServletResponse response,MemberCartVO memberCartVO, MemberVO memberVO,String cart_ids,String address_id )throws Exception{
		//验证参数
		if(cart_ids==null || "".equals(cart_ids)){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		if(!StringUtil.checkIds(cart_ids)){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		
		boolean re =memberCartServise.checkBuyGoosNumBySthandardTab(cart_ids);
        
		if(!re){
			return PackageUtil.packageParams(10201,"库存不足","",""); 
		}
		
		Map<String, Object> map = new HashMap();
		memberVO =memberService.getAllInfoById(member_id);
		map.put("memberLevel",memberVO.getLevel());
		
		//查询地址
		AddressVO addressVO=new AddressVO();
		if(address_id==null || "".equals(address_id)){
			//可以没有默认地址 使用第一个
			List<AddressVO> addressList = addressService.getAddressListByMemberId(member_id);
			if(addressList.size() != 0){
				addressVO = addressList.get(0);
			}
		}else{
			addressVO=addressService.getAlternativeAddress(address_id);
		}
		if(null==addressVO){
			addressVO=new AddressVO();
		}
		map.put("address", addressVO);
		//查询兑换券余额
		int ticketTotal = memberTicketService.getTotalTicket(member_id);
		map.put("total_ticket", ticketTotal);
		//查询商品可使用商票  //ticket
		List<MemberCartVO> l = memberCartServise.packageCartParam(cart_ids);
		
		//过滤出从购物车传过来的商品里 纯现金购买的商品
		List<MemberCartVO> allMoneyGoods=memberCartServise.packageCartAllMoneyGoodsParam(l);
		//过滤出从购物车传过来的商品里 除去纯现金购买的商品
		List<MemberCartVO> notAllMoneyGoods=memberCartServise.packageCartNotAllMoneyGoodsParam(l);
		
		double allMoneyGoodsPriceTotal	=0;
		if(allMoneyGoods.size()>0){
			//纯现金商品总价
		    allMoneyGoodsPriceTotal	= goodStandardService.getCartTotalPrice(allMoneyGoods);
		}
		
		int total = goodStandardService.getCartTotalTickets(l);
		map.put("ticket_use", total);
		//所有商品不使用兑换券总价  sell_price
		double priceTotal = goodStandardService.getCartTotalPrice(l);
		
		double ticketPriceTotal =0;
		if(notAllMoneyGoods.size()>0){
			//使用券后总价  ticket_price
			ticketPriceTotal	= goodStandardService.getCartTicketPrice(notAllMoneyGoods);
		}
		ticketPriceTotal = ticketPriceTotal+allMoneyGoodsPriceTotal;
		map.put("total_price", priceTotal);
		map.put("ticket_price", ticketPriceTotal);
		map.put("ticket_use", total);
		//查询商品列表
		List<MemberCartVO> memberCartList=memberCartServise.selectmemberCartListBuy(cart_ids,member_id);
		for (int i = 0; i < memberCartList.size(); i++) {
			memberCartList.get(i).setCompany_name("友米乐自营");
		}
		//判断商品列表里的商品是不是都是现金购买商品  Y=全是现金购买商品
		String buy_type=memberCartServise.checkGoodsBuyType(memberCartList);
		map.put("good_list", memberCartList);
		//运费
		
		String standards[]=new String[l.size()];
		String numes[]=new String[l.size()];
		for(int i=0;i<l.size();i++){
			standards[i]=l.get(i).getGood_standard_id();
			numes[i] = l.get(i).getNum();
		}
		double feight =0;
		if(addressVO.getAddress_id() !=null){
			feight = goodTempService.calculateFeight(standards,numes,addressVO.getAddress_id());
		}
		map.put("feight",feight);
		if("127".equals(memberVO.getLevel()) ||"126".equals(memberVO.getLevel()) ||"Y".equals(buy_type)){
			map.put("ticket_show_yn","0");
		}else{
			map.put("ticket_show_yn","1");
		}
		return PackageUtil.packageParams(200,"successs",map,""); 
	}
	
	
	/**立即购买跳结算页面
	 * @param token
	 * @param member_id
	 * @param num
	 * @param standard_id
	 * @param address_id
	 */
	@ResponseBody
	@RequestMapping(value="/fillOrderOne",method=RequestMethod.GET)
	public Object fillOrderOne(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request
			, HttpServletResponse response,MemberCartVO memberCartVO, MemberVO memberVO, String standard_id,String num,String address_id)throws Exception{
		//验证参数
		if(standard_id==null || "".equals(standard_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		
		if(!StringUtil.checkIds(standard_id) || !StringUtil.checkIds(num)){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		GoodStandardVO checkGood = goodStandardService.checkGood(standard_id);
		if(checkGood ==	null){
			return PackageUtil.packageParams(10101,"商品已下架或删除","",""); 
		}
		if("0".equals(checkGood.getNum())){
			return PackageUtil.packageParams(10201,"库存不足","","");
		}
		if(Integer.parseInt(checkGood.getNum())<Integer.parseInt(num)){
			return PackageUtil.packageParams(10201,"库存不足","","");
		}
		
		Map<String, Object> map = new HashMap();

		//memberVO = memberService.getLevelByMemberId(member_id);
		memberVO =memberService.getAllInfoById(member_id);

		map.put("memberLevel",memberVO.getLevel());
		
		//查询地址
		AddressVO addressVO = null;
		if(address_id==null || "".equals(address_id)){
//			addressVO = addressService.getDefaultAddress(member_id);
			//可以没有默认地址 使用第一个
			List<AddressVO> addressList = addressService.getAddressListByMemberId(member_id);
			if(addressList.size() != 0){
				addressVO = addressList.get(0);
			}
		}else{
			addressVO=addressService.getAlternativeAddress(address_id);
		}
		if(null==addressVO){
			addressVO=new AddressVO();
		}
		map.put("address", addressVO);
		//查询兑换券余额
		int ticketTotal = memberTicketService.getTotalTicket(member_id);
		map.put("total_ticket", ticketTotal);
		//查询商品可使用商票
		int total = goodStandardService.getCartTotalTicketsByBuyOne(standard_id,num);
		map.put("ticket_use", total);
		//查询节省的金额
		//总价
		double priceTotal = goodStandardService.getCartTotalPriceByBuyOne(standard_id,num);
		//使用券后总价
		double ticketPriceTotal = goodStandardService.getCartTicketPriceByBuyOne(standard_id,num);
		map.put("total_price", priceTotal);
		map.put("ticket_price", ticketPriceTotal);
		map.put("ticket_use", total);
		
		//查询现金余额
		double  cash_money= memberService.getfreeCash(member_id);
		map.put("cash_money",cash_money);
		//查询商品列表
		List<MemberCartVO> memberCartList=memberCartServise.selectmemberCartListBuyOne(standard_id,num);
		for (int i = 0; i < memberCartList.size(); i++) {
			memberCartList.get(i).setCompany_name("友米乐自营");
		}
		//判断商品列表里的商品是不是都是现金购买商品  Y=全是现金购买商品
		String buy_type=memberCartServise.checkGoodsBuyType(memberCartList);
		map.put("good_list", memberCartList);
		//运费
		String[] standards = null;
		String[] numes = null;
		standards = standard_id.split(",");
		numes = num.split(",");
		double feight =0;
		if(addressVO!=null){
			feight = goodTempService.calculateFeight(standards,numes,addressVO.getAddress_id());
		}
		map.put("feight", feight);
		if("127".equals(memberVO.getLevel()) ||"126".equals(memberVO.getLevel()) ||"Y".equals(buy_type)){
			map.put("ticket_show_yn","0");
		}else{
			map.put("ticket_show_yn","1");
		}
		return PackageUtil.packageParams(200,"successs",map,""); 
	}
	/**获取购物车数量
	 * @param token
	 * @param member_id
	 */
	@ResponseBody
	@RequestMapping(value="/cartGoodNum",method=RequestMethod.GET)
	public Object cartGoodNum(@RequestHeader(value = "LANG", required = false) String lang,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request
			, HttpServletResponse response,MemberCartVO memberCartVO, MemberVO memberVO, String standard_id)throws Exception{
		
		String cartGoodNum = memberCartServise.selectCartGoodNum(member_id,lang);
		 return PackageUtil.packageParams(200,"购物车数量",cartGoodNum,""); 
	}
	/**
	 * 加入购物车
	 * @param standard_id 规格ID
	 * @param num 数量
	 */
	@ResponseBody
	@RequestMapping(value="/addCart",method=RequestMethod.POST)
	public Object addCart(@RequestHeader(value = "LANG", required = false) String lang,String standard_id,HttpServletRequest request, HttpServletResponse response,@RequestHeader(value = "AUTHORIZATION", required = false) String token,
			@RequestHeader(value = "SECRET", required = false) String member_id, MemberVO memberVO, String num) throws Exception{
	
		//验证数量
		if(num==null || "".equals(num)||!StringUtil.isNumeric(num)){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if(lang!=null && !"".equals(lang)){
			String country = goodService.checkCountry(standard_id);
			if("1".equals(country)){
				return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
			}
		}		
		//验证商品ID
		int checkNum = goodService.checkGoodStandardById(standard_id);
		if(checkNum==0){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		
		//检查库存是否充足
		int re = memberCartServise.checkMaxNum(standard_id,member_id,num);
		if(re > 0){
			return PackageUtil.packageParams(10201,"库存不足","",""); 
		}
		//检查商品的状态
		String  good_state = goodStandardService.selectGoodState(standard_id);
		if(!"1".equals(good_state)){
			return PackageUtil.packageParams(10101,"商品已下架或删除","",""); 
		}
		
		//验证加入的商品购物车数量是不是超出100件
		int cart_cunt =  memberCartServise.getCartNum(member_id);
		if(cart_cunt>=100){
			return PackageUtil.packageParams(10202,"购物车商品数量加载超限","",""); 
		}
		//检查商品是否在购物车里存在
		String id = memberCartServise.checkCartExsit(standard_id,member_id);
		if(id==null){
			memberCartServise.addCart(standard_id,member_id,num,lang);
		}else{
			memberCartServise.addCartNum(id,num);
		}
		return PackageUtil.packageParams(200,"success","","");
	}

	/**
	 * @param cart_id              购物车ID 
	 * @param ticket_status        状态  0 未使用购物券   1使用购物券
	 * @param address_id           地址ID  
	 * @param order_from           状态   1APP  2微信     3网站
	 * @param cart_status          状态   0没有购物车立即下单   1购物车下单
	 * @param standard_ids         商品规格id	
	 * @param standard_nums        商品规格数量 
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value="/insertOrder",method=RequestMethod.POST)
	public Object insertOrder(@RequestHeader (value="LANG", required=false)String lang,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request
			, HttpServletResponse response,MemberCartVO memberCartVO, MemberVO memberVO, String cart_id,String ticket_status,String address_id,String order_from,String cart_status,String standard_ids,String standard_nums,String address_obj_en)throws Exception{
		//判断非空___________________________________________________________________________________________________________________start
		if(cart_status==null||(!"0".equals(cart_status)&&!"1".equals(cart_status))){
			return PackageUtil.packageParams(0,"请选择下单类型","",""); 
		}
		if(ticket_status==null||(!"0".equals(ticket_status)&&!"1".equals(ticket_status))){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		if("0".equals(cart_status)){
			if(standard_ids==null || "".equals(standard_ids)){
				return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
			}
			if(standard_nums==null || "".equals(standard_nums)){
				return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
			}
			if(Integer.parseInt(standard_nums)<1){
				return PackageUtil.packageParams(10001,"SB","",""); 
			}
		}
		AddressVO addressVO = new AddressVO();
		String country = "";
		if(lang==null){
			country = "1";
			addressVO = memberCartServise.getMemberAddressById(address_id);
		}else{
			if(address_obj_en==null){
				return PackageUtil.packageParams(10901,"收货地址错误","",""); 
			}
			country = "0";
			addressVO.setAddress_obj_en(address_obj_en);
			addressVO.setAddress_id(address_id);
		}
		
		MemberVO member = memberCartServise.getMemberById(member_id);
		if(addressVO==null){
			return PackageUtil.packageParams(10901,"收货地址错误","",""); 
		}
		//判断非空___________________________________________________________________________________________________________________end
		String os =  request.getHeader("OS");
		if (os != null) {
			os = os.toLowerCase();
		}
		order_from = "1";
		if("wechat".equals(os)){
			order_from = "2";
		}
		else if("web".equals(os)){
			order_from = "3";
		}
		else if("ios".equals(os)||"android".equals(os)){
			order_from = "1";
		}
		System.out.println("下单开始");
		Map map = orderService.addPlaceOrder(member,addressVO,cart_status,cart_id,ticket_status,order_from,standard_ids,standard_nums,country);
		System.out.println("下单结束");
		return map;
	}
	
	/**
	 * 促销活动列表底部返回商品促销信息
	 * @param token
	 * @param member_id
	 * @param promotion_id
	 */
	@ResponseBody
	@RequestMapping(value="/returnPromotionInfo",method=RequestMethod.GET)
	public Object returnPromotionInfo(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response,
			MemberCartVO memberCartVO, MemberVO memberVO,String promotion_id)throws Exception{
		
		Map<String, Object> map = new HashMap();
		List<PromotionItemVO>  promotionItemList =promotionService.selectPromotionItemList(promotion_id);
		String contentStr=promotionService.getContenStr(promotion_id,member_id);
		map.put("contentStr", contentStr);
		map.put("promotionItemList", promotionItemList);
		
		return PackageUtil.packageParams(200,"successs",map,""); 
	}
	
	
	/**
	 *  结算页(购物车)
	 * @param token
	 * @param member_id
	 * @param request
	 * @param response
	 * @param cart_ids
	 * @param address_id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/cartToAccounts",method=RequestMethod.GET)
	public Object cartToAccounts(@RequestHeader(value = "LANG", required = false) String lang,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response,
			String cart_ids,String address_id){
	
		//验证参数
		if(cart_ids==null || "".equals(cart_ids)){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		if(!StringUtil.checkIds(cart_ids)){
			return PackageUtil.packageParams(10000,"参数错误，请参考API文档","",""); 
		}
		MemberVO memberVO =memberService.getAllInfoById(member_id);
		//查询兑换券余额
		int ticketTotal = memberTicketService.getTotalTicket(member_id);
//		AddressVO addressVO =memberCartServise.getMemberCartAddress(member_id,address_id);
		AddressVO addressVO = memberService.getMemberAddress(member_id,address_id,lang);
		if(addressVO==null){
			return PackageUtil.packageParams(10901,"收货地址错误","",""); 
		}
		int cartNums = memberCartServise.checkCartsStandardNums(cart_ids);
		if(cartNums>0){
			return PackageUtil.packageParams(10201,"库存不足","",""); 
		}
//		Map<String,String> imap =new HashMap<String,String>();
//		imap.put("cart_ids", cart_ids);
//		int limitedNums = memberCartServise.checkLimitedNums(imap);
//		if(limitedNums>0){
//			return PackageUtil.packageParams(10201,"库存不足","",""); 
//		}
		Map map = memberCartServise.cartToAccounts(cart_ids,memberVO,addressVO,ticketTotal,lang);
		return PackageUtil.packageParams(200,"successs",map,""); 
	}
	
	/**
	 * 结算页(立即购买)
	 * @param token
	 * @param member_id
	 * @param request
	 * @param response
	 * @param standard_id
	 * @param num
	 * @param address_id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/buyNowToAccounts",method=RequestMethod.GET)
	public Object buyNowToAccounts(@RequestHeader(value = "LANG", required = false) String lang,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response,
			String standard_id,String num,String address_id){
	
		//验证参数
		if(num==null||"".equals(num)||standard_id==null||"".equals(standard_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","",""); 
		}
		MemberVO memberVO = memberService.getAllInfoById(member_id);
		//查询兑换券余额
		int ticketTotal = memberTicketService.getTotalTicket(member_id);
		/*AddressVO addressVO =memberCartServise.getMemberCartAddress(member_id,address_id);*/
		
		AddressVO addressVO = memberService.getMemberAddress(member_id,address_id,lang);
		if(addressVO==null){
			return PackageUtil.packageParams(10901,"收货地址错误","",""); 
		}
		GoodStandardVO checkGood = goodStandardService.checkGood(standard_id);
		if(checkGood ==	null){
			return PackageUtil.packageParams(10101,"商品已下架或删除","",""); 
		}
		if("0".equals(checkGood.getNum())){
			return PackageUtil.packageParams(10201,"库存不足","","");
		}
		if(Integer.parseInt(checkGood.getNum())<Integer.parseInt(num)){
			return PackageUtil.packageParams(10201,"库存不足","","");
		}
		String limitedNums = memberCartServise.checkLimitedNumsByStandardId(standard_id);
		if(limitedNums !=null){
			if(Integer.parseInt(num)>Integer.parseInt(limitedNums)){
				return PackageUtil.packageParams(10201,"库存不足","",""); 
			}
		}
		Map map = memberCartServise.buyNowToAccounts(standard_id,num,memberVO,addressVO,ticketTotal,lang);
		return PackageUtil.packageParams(200,"successs",map,""); 
		
	}
	/**
	 * 新手礼包支付
	 * @param token 令牌
	 * @param member_id 用户ID
	 * @param package_id 套餐
	 * @param standard_id 
	 * @param address_id
	 * @throws Exception 
	 */
	@RequestMapping(value="/settlementPackageGoodsOne",method=RequestMethod.GET)
	public String settlementPackageGoodsOne(@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="OS", required=false) String os,
			@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, 
			HttpServletResponse response, MemberVO memberVO,String level, String package_id,String address_id,String standard_id) throws Exception{
		/*member_id="3298";	
		level="125";
		os="ios";
		package_id="89";
		standard_id="1789";*/
		if(os != null){
			os = os.toLowerCase();
		}
		AddressVO defaultAddress =new AddressVO();
		MemberVO m =  new MemberVO();
		PackageVO p = new PackageVO();
		String address =null;
		
		memberVO = memberService.getLoginMemberById(member_id);
		level = PropertieUtil.loadPropertie("primary_level");
		String rate = memberItemService.getRateByLevel(PropertieUtil.loadPropertie("primary_level"));
		DicVO dicVO = dicService.getDicById(level);
		m.setLevel_name(dicVO.getName());
		//购买套餐
		p = packageService.getPackageByPackageIdAndStandardId(package_id,standard_id);
		m.setRecharge_ticket(p.getRecharge_ticket());
		standard_id= p.getGood_list().get(0).getStandard_id();
		m.setRate(rate);
		p.setDefaultPrice(p.getGood_list().get(0).getDefault_price());
		
		//收获地址
		if(address_id !=null){
			defaultAddress = addressService.getAddressById(address_id);
		}else{
			defaultAddress = addressService.getDefaultAddress(member_id);
		}
		if(defaultAddress !=null){
			 address = addressService.tempAddress(defaultAddress);
			 address_id = defaultAddress.getAddress_id();
		}
		request.setAttribute("level", level);
		request.setAttribute("standard_id", standard_id);
		request.setAttribute("packageOne", p);
		request.setAttribute("level_up_info", m);
		request.setAttribute("package_buy", 1);
		request.setAttribute("ticket_logo", configService.loadConfig("TICKET_LOGO"));
		request.setAttribute("free_cash", memberVO.getFree_cash());
		request.setAttribute("address_id", address_id);
		request.setAttribute("address", address);
		request.setAttribute("os", os);
		request.setAttribute("memberId", member_id);
		request.setAttribute("token", token);
		request.setAttribute("free_cash", memberVO.getFree_cash());
		
		return "/app/memberCart/settlementPackageGoodsOne";
	}
}
